package com.hxc.controller;

import cn.hutool.jwt.JWT;
import com.github.pagehelper.PageInfo;
import com.hxc.ann.Syslog;
import com.hxc.pojo.Area;
import com.hxc.pojo.Main;
import com.hxc.pojo.Stadium;
import com.hxc.pojo.Type;
import com.hxc.service.MainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RequestMapping("main")
@Controller
public class MainController {

    @Autowired
    MainService mainService;

    /**
     * 列表
     * @param main
     * @param pageNum
     * @param pageSize
     * @param model
     * @return
     */
    @RequestMapping("list")
    public String list(Main main, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "3") Integer pageSize, Model model){
        PageInfo<Main> mainPageInfo = mainService.listAll(main, pageNum, pageSize);
        model.addAttribute("pg",mainPageInfo);
        model.addAttribute("main",main);
        return "list";
    }

    /**
     * 获取场馆
     * @return
     */
    @ResponseBody
    @RequestMapping("getStadium")
    public List<Stadium> getStadium(){
        return mainService.getStadium();
    }

    /**
     * 获取项目类型
     * @return
     */
    @ResponseBody
    @RequestMapping("getType")
    public List<Type> getType(){
        return mainService.getType();
    }

    /**
     * 获取地区
     * @return
     */
    @ResponseBody
    @RequestMapping("getArea")
    public List<Area> getArea(){
        return mainService.getArea();
    }

    /**
     * 跳转到添加页面
     * @return
     */
    @RequestMapping("toAdd")
    public String toAdd(){
        return "add";
    }

    /**
     * 添加页面
     * @return
     */
    @Syslog(value = "添加")
    @RequestMapping("add")
    public String add(Main main, HttpServletRequest request){
        Object uid = "";
        Cookie[] cookies = request.getCookies();
        if (!StringUtils.isEmpty(cookies)){
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals("login_token")){
                    JWT jwt = JWT.of(cookie.getValue());
                    uid = jwt.getPayload("uid");
                }
            }
        }
        main.setUid(Integer.parseInt(uid.toString()));
        int i = mainService.addMain(main);
        return "redirect:/main/list";
    }

    /**
     * 跳转到报表页面
     * @return
     */
    @RequestMapping("toCountNum")
    public String toCountNum(){
        return "countNum";
    }

    @ResponseBody
    @RequestMapping("getNum")
    public List<Main> getNum(){
        List<Main> mains = mainService.countNum();
        System.out.println(mains);
        return mains;
    }
}
